import java.net.*;
import java.io.*;
public class Webcontent  { 

	/**Author:Svtpahal
	 * Feb 05,2009
	 * @Program returns the Http status code & response message of a Url and also contents of web page in a xml file.
	 **/
	private int responseCode;
	private String responseMessage;
	private String type;
	private int length;
	//private java.util.Map<String,java.util.List<String>> responseHeader = null;
	//Open a web file. 
	public Webcontent(String urlstring)throws MalformedURLException, IOException
	{
		//Open a URL connection.
		URL url=new URL(urlstring);
		URLConnection ucon=url.openConnection();
		HttpURLConnection con=(HttpURLConnection)ucon;
		//Set up a request.
		con.setConnectTimeout( 10000 );
		con.setReadTimeout( 10000 );
		con.setInstanceFollowRedirects( true );
		con.setRequestProperty( "User-agent", "spider" );
		//Send the request.
		con.connect();
		//Get the response.
		responseCode= con.getResponseCode( );
        responseMessage=con.getResponseMessage();
        url= con.getURL( );
        length=con.getContentLength();
        type  = con.getContentType();
        
        //Get the content.
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String str;
        BufferedWriter out = new BufferedWriter(new FileWriter("outfile.xml"));
        while ((str=in.readLine()) != null) {
            out.write(str);
          }
        
        in.close();
        out.close();
        //return responseCode;
	}
		
	//get the response message.
	public String getResponseMessage() {
		return responseMessage;
	}
	public void setResponseMessage(String responseMessage) {
		this.responseMessage = responseMessage;
	}
	//get the content length.
	public int getLength() {
		return length;
	}
	public void setLength(int length) {
		this.length = length;
	}
	//get the response code.
	public int getResponseCode() {
		return responseCode;
	}
	public void setResponseCode(int responseCode) {
		this.responseCode = responseCode;
	}
	//get the MIME type.
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	
	 
}
